<?php
namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * Select 下拉数据
 * @internal
 */
class Select extends Backend
{
    // 产品列表
    public function getProduct()
    {
        $limit = $this->request->param('pageSize') ?: 10;
        $page = $this->request->param('pageNumber') ?: 1;
        $id = $this->request->param('keyValue') ?:0;

        $productLists = Db::name('product')->where(['status' => 1,'is_delete' => 0])
            ->where(function ($sql) use($id) {
                if (!empty($id)) {
                    $sql->where('id', $id);
                }
            })
            ->field('id,name')->order('id','desc')
            ->limit($limit)->page($page)->select();
        if (!empty($productLists)) foreach ($productLists as &$list) {
            $list['name'] = $list['name']."（ID:{$list['id']}）";
        }

        $total = Db::name('product')->where(['status' => 1,'is_delete' => 0])->field('id,name')->count();

        $result = [
            'code' => 0,
            'msg' => '',
            'total' => $total,
            'list' => $productLists
        ];

        return json($result);

//        $result = array("total" => $total, "rows" => array_values($productLists1));
//        return $result;
    }
}